package com.example.zbh.dsliao.ui.activty;

import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.easemob.chat.EMChatManager;
import com.easemob.chat.EMGroup;
import com.easemob.chat.EMGroupManager;
import com.easemob.chat.GroupChangeListener;
import com.easemob.exceptions.EaseMobException;
import com.example.zbh.dsliao.R;
import com.example.zbh.dsliao.constant.Constant;
import com.example.zbh.dsliao.ui.adapter.GroupAdapter;

import org.w3c.dom.ls.LSException;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by zbh on 15/11/23.
 */
public class GroupsActivity extends BaseActivity implements View.OnClickListener, AdapterView.OnItemClickListener {
    private ListView group_listView;
    private List<EMGroup> list = new ArrayList<>();
    private GroupAdapter groupAdapter;
    private TextView Create_new_groups, add_public_groups;//新建群组
    private LinearLayout back;
    private List<EMGroup> listS = new ArrayList<>();

    @Override
    protected int getLayOut() {
        return R.layout.activity_groups;
    }

    @Override
    protected void initView() {
        group_listView = findeView(R.id.group_listView);
        View headerView = LayoutInflater.from(this).inflate(R.layout.group_listview_header, null);
        Create_new_groups = (TextView) headerView.findViewById(R.id.Create_new_groups);
        add_public_groups = (TextView) headerView.findViewById(R.id.add_public_groups);
        Create_new_groups.setOnClickListener(this);
        add_public_groups.setOnClickListener(this);
        group_listView.addHeaderView(headerView);
        back = findeView(R.id.back);

    }

    @Override
    protected void initData() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    EMGroupManager.getInstance().getGroupsFromServer();
                    list = EMGroupManager.getInstance().getAllGroups();
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            groupAdapter = new GroupAdapter();
                            group_listView.setAdapter(groupAdapter);
                            groupAdapter.addData(list);

                        }
                    });
                } catch (EaseMobException e) {
                    e.printStackTrace();
                }
            }
        }).start();
        group_listView.setOnItemClickListener(this);
        back.setOnClickListener(this);
        EMGroupManager.getInstance().addGroupChangeListener(new GroupChangeListener() {
            @Override
            public void onInvitationReceived(String s, String s1, String s2, String s3) {

            }

            @Override
            public void onApplicationReceived(String s, String s1, String s2, String s3) {
                upGroupList();
            }

            @Override
            public void onApplicationAccept(String s, String s1, String s2) {

            }

            @Override
            public void onApplicationDeclined(String s, String s1, String s2, String s3) {

            }

            @Override
            public void onInvitationAccpted(String s, String s1, String s2) {

            }

            @Override
            public void onInvitationDeclined(String s, String s1, String s2) {

            }

            @Override
            public void onUserRemoved(String s, String s1) {
                upGroupList();
            }

            @Override
            public void onGroupDestroy(String s, String s1) {
                upGroupList();
            }
        });

    }


    public void upGroupList() {
        new Thread(new Runnable() {
            @Override
            public void run() {

                try {
                    EMGroupManager.getInstance().getGroupsFromServer();
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {

                            list = EMGroupManager.getInstance().getAllGroups();
                            groupAdapter.addData(list);
                        }
                    });
                } catch (EaseMobException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.Create_new_groups:
                toNewGroupActivity();
                break;
            case R.id.add_public_groups:

                break;
            case R.id.back:
                finish();
                break;
        }

    }

    private void toNewGroupActivity() {
        Intent intent = new Intent();
        intent.setClass(this, NewGroupActivity.class);
        startActivity(intent);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Intent intent = new Intent();
        intent.setClass(this, GroupChatActivity.class);
        String name = list.get(position - 1).getGroupId();
        Log.i("nannanananansssss", list.get(position - 1).getGroupName() + ">>>>>>" + list.get(position - 1).getGroupId());
        Log.i("nanchaodong", name);
        intent.putExtra(Constant.GROUPUSERID, name);
        intent.putExtra(Constant.GROUPNAME, list.get(position - 1).getGroupName());
        startActivity(intent);

    }
}
